package com.translate.web.service.impl;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import com.basic.service.impl.BaseServiceImpl;
import com.page.PageResponseData;
import com.param.contract.IPage;
import com.translate.entity.Notice;
import com.translate.entity.NoticeNum;
import com.translate.web.dao.NoticeMapper;
import com.translate.web.param.contract.IUserIdParam;
import com.translate.web.service.INoticeService;

@Service
public class NoticeServiceImpl extends BaseServiceImpl<Notice> implements INoticeService {

	public NoticeServiceImpl(NoticeMapper mapper) {
		super(mapper);
	}

	protected void insertPropteris(Notice data) {
		long time = System.currentTimeMillis() / 1000;
		data.setCreateTime(time);
		data.setUpdateTime(time);
	}

	protected void updatePropteris(Notice data) {
		data.setUpdateTime(System.currentTimeMillis() / 1000);
	}

	protected Class<Notice> getEntityClass() {
		return Notice.class;
	}

	public <S extends IPage, VO> PageResponseData<VO> getDataByList(S pageParam, Class<VO> targetClass) {

		PageResponseData<Notice> pageResponseData = this.getDataByList(pageParam);

		long total = pageResponseData.getTotal();

		if (0 == total) {
			return new PageResponseData<>();
		}
		return new PageResponseData<>(total, entityConvertToVo(pageResponseData.getData(), targetClass),
				pageResponseData.getPageSize());
	}

	@Override
	public <S extends IPage> PageResponseData<Notice> getDataByList(S pageParam) {

		NoticeMapper mapper = this.getBaseMapper();

		IUserIdParam userIdParam = (IUserIdParam)pageParam;
		
		List<NoticeNum> countNotice = mapper.countNotice(userIdParam.getUserId());

		int num = 0;
		
		for (NoticeNum noticeNum : countNotice) {
			num += noticeNum.getNum();
		}
		
		if (0 == num) {
			return new PageResponseData<>(0, new ArrayList<>(0), pageParam.getSelectCount());
		}

		List<Notice> selectNotice = mapper.selectNotice(pageParam);

		duplicateRemoval(selectNotice);

		return new PageResponseData<>(num, selectNotice, pageParam.getSelectCount());

//		IBaseMapper<T> baseMapper = this.getBaseMapper();
//		int count = this.getCountByDataList(pageParam);
//
//		if (0 == count) {
//			return new PageResponseData<>(0, new ArrayList<>(0));
//		}
//
//		List<T> list = baseMapper.getDataByList(pageParam);
//
//		duplicateRemoval(list);
//		
//		
//		return new PageResponseData<>(count, list);
	}

	@Override
	public int countNumber(int userId) {

		NoticeMapper mapper = this.getBaseMapper();

		// 群发的
		int countAllNumByGroup = mapper.countAllNumByGroup();
		
		// 发给个人的
		int countAllNumByPerson = mapper.countAllNumByPerson(userId);
		
		// 群发的+ 发给个人的 - 已读的
		int countAlreadyReadNum = mapper.countAlreadyReadNum(userId);
		return countAllNumByGroup +countAllNumByPerson -  countAlreadyReadNum;
	}

//	@Override
//	protected <S extends IPage> int count(Connection connection, S pageParam) {
//		// TODO Auto-generated method stub
//		NoticeMapper mapper = this.getBaseMapper();
//		
//		return mapper.countByParam(pageParam);
//	}

}